//
//  YMMyView.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/3/14.
//

import SwiftUI
import SDWebImageSwiftUI
private let fav    = "我的收藏"
private let down   = "我的下载"
private let browse = "浏览记录"
private let about  = "关于我们"
private let set    = "设置"

struct YMMyView: View {
    @EnvironmentObject var client :YMClient
    @State private var user :YMUser = YMUser()
    @State private var listData :[YMMyModel] = []
    @State private var isLogin :Bool = false
    var body: some View {
        VStack {
            List(listData,id: \.self) { model in
                if model.title == fav  {
                    Section {
                        HStack(alignment: .center, spacing: 20) {
                            WebImage(url: URL(string: user.corver)).resizable().placeholder {
                                RoundedRectangle(cornerRadius:35).foregroundColor(.gray)
                            }.cornerRadius(35).frame(width: 70, height: 70, alignment: .center)
                            Text(user.nickName).font(Font.system(size: 20)).foregroundColor(Color.appx000000()).lineLimit(1)
                        }.padding(EdgeInsets(top: 20, leading: 0, bottom: 20, trailing: 0))
                    }.onTapGesture {
                        if !YMUser.loginIn{
                            self.isLogin = true
                        }else{

                        }
                    }.fullScreenCover(isPresented: $isLogin) {

                    } content: {
                        YMLoginView(isLogin: $isLogin)
                       // YMLoginView.init(client: <#T##EnvironmentObject<YMClient>#>, isLogin: <#T##Binding<Bool>#>, hudVisible: <#T##Bool#>)
                    }
                }
                YMMyViewCell(model: model)
            }
            .listStyle(.insetGrouped).onAppear {
                self.loadData()
            }.onChange(of: isLogin) { login in
                debugPrint(login)
                debugPrint(user.nickName,user.corver)
            }
        }
    }
    func loadData(){
        let item1 = YMMyModel(title: fav, icon: "icon_fav")
        let item2 = YMMyModel(title: down, icon: "icon_down")
        let item3 = YMMyModel(title: browse, icon: "icon_historys")
        let item4 = YMMyModel(title: about, icon: "icon_option")
        let item5 = YMMyModel(title: set, icon: "icon_set")
        self.listData = [item1,item2,item3,item4,item5]
        user = moya.user
    }
}
struct YMMyViewCell :View{
    @State var model :YMMyModel
    var body :some View{
        VStack.init {
            NavigationLink {
                switch model.title {
                case down:
                    YMDownView()
                case fav:
                    YMFavView()
                case set:
                    YMSetView()
                case browse:
                    YMHistoryView()
                default:
//                    YMAboutView().navigationBarBackButtonHidden(true).navigationBarItems(leading: BaseNavigationBarBackButton()).navigationBarTitle(model.title)
                    YMNavigationViewController(vc: YMAboutController()).navigationBarHidden(true)//navigationBarBackButtonHidden(true).navigationBarItems(leading: BaseNavigationBarBackButton()).navigationBarTitle(model.title)
                   // Text(model.title).navigationBarBackButtonHidden(true).navigationBarItems(leading: BaseNavigationBarBackButton()).navigationBarTitle(model.title)
                }
            } label: {
                HStack(alignment: .center, spacing: 0) {
                    Image(model.icon)
                    Text(model.title).padding().font(.system(size: 16)).foregroundColor(Color.appx000000())
                }
            }
        }
    }
}
struct YMMyModel :Hashable{
    var title :String
    var icon  :String
}
